﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FSY.API.Utilities;
using FSY.Components.Entities;
using FSY.Globals;
using FSY.API.Files;

namespace FSY.Web.Admin
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
                AuthorizationUser();
            LoadUserControl();
        }

        private void AuthorizationUser()
        {
            if (Session["IsSuperAdmin"] != null)
                return;
            if(Session["UserID"] != null)
            {
                int userID = Fsy_Null.SetNullInteger(Session["UserID"]);
                List<Fsy_RoleQuery> lsroleQ = Fsy_RoleController.GetRoleByUser(userID);
                if (lsroleQ == null || lsroleQ.Count == 0)
                {
                    Server.Transfer("Login.aspx");                    
                }

                foreach (Fsy_RoleQuery roleQ in lsroleQ)
                {
                    if (roleQ.RoleName != "admin" && roleQ.RoleName != "superadmin")
                    {
                        Server.Transfer("Login.aspx");                        
                    }
                }
            }
            if (Session["UserID"] == null)
            {
                Server.Transfer("Login.aspx");                
            }
        }

        private void LoadUserControl()
        {
            int tabid = -1;
            if (Request.QueryString["tabid"] != null)
            {
                tabid = Fsy_Null.SetNullInteger(Request.QueryString["tabid"]);
                switch (tabid)
                {
                    case 1:
                        LoadControlForAdmin();
                        break;
                    case 2:
                        LoadControlForHost();
                        break;
                    case 3:
                        break;
                    default:                        
                        break;
                }
            }
        }

        private void LoadControlForAdmin()
        {
            string obj = string.Empty;
            if (Request.QueryString["obj"] != null)
            {
                obj = Fsy_Null.SetNullString(Request.QueryString["obj"]);
                switch (obj)
                {
                    case "menu":
                        break;
                    case "module":
                        break;
                    case "theme":
                        break;
                    case "role":
                        break;                   
                    default:
                        break;
                }
            }
        }

        private void LoadControlForHost()
        {
            string obj = string.Empty;
            if (Request.QueryString["obj"] != null)
            {
                obj = Fsy_Null.SetNullString(Request.QueryString["obj"]);
                switch (obj)
                {
                    case "portal":
                        pnlContent.Controls.Add(LoadControl(Fsy_FileController.GetVitualFilePath("Admin/Portals/Manager.ascx")));
                        break;
                    case "menu":
                        pnlContent.Controls.Add(LoadControl(Fsy_FileController.GetVitualFilePath("Admin/Menus/Manager.ascx")));
                        break;
                    case "module":
                        pnlContent.Controls.Add(LoadControl(Fsy_FileController.GetVitualFilePath("Admin/Modules/Manager.ascx")));
                        break;
                    case "theme":
                        pnlContent.Controls.Add(LoadControl(Fsy_FileController.GetVitualFilePath("Admin/Themes/Manager.ascx")));
                        break;
                    case "role":
                        pnlContent.Controls.Add(LoadControl(Fsy_FileController.GetVitualFilePath("Admin/Securities/Manager.ascx")));
                        break;
                    case "perm":
                        pnlContent.Controls.Add(LoadControl(Fsy_FileController.GetVitualFilePath("Admin/Securities/Manager.ascx")));
                        break;
                    case "user":
                        pnlContent.Controls.Add(LoadControl(Fsy_FileController.GetVitualFilePath("Admin/Users/Manager.ascx")));
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
